﻿using System;
using Gadgeteer.Interfaces;
using Microsoft.SPOT;

using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using GTI = Gadgeteer.Interfaces;

namespace Gadgeteer.Modules.Sytech
{
    /// <summary>
    /// A Accel3Axis Gadgeteer module
    /// 
    /// This is the initial firmware release for the 3 Axis Accellorometer.
    /// It gives basic acess to the module
    /// This only supports reading the x,y and z axis.
    /// 
    /// No interupts or trigger levels supported yet
    /// </summary>
    /// <example>
    /// <code>
    /// using System;
    ///using System.Threading;
    ///using Microsoft.SPOT;
    ///using Microsoft.SPOT.Presentation;
    ///using Microsoft.SPOT.Presentation.Controls;
    ///using Microsoft.SPOT.Presentation.Media;
    ///using Microsoft.SPOT.Touch;

    ///using Gadgeteer.Networking;
    ///using GT = Gadgeteer;
    ///using GTM = Gadgeteer.Modules;
    ///using Gadgeteer.Modules.Sytech;
    ///using Timer = Gadgeteer.Timer;

    ///namespace AcellTest
    ///{
    ///    public partial class Program
    ///    {
    ///        // This template uses the NANAO mainboard from Sytech Designs
    ///
    ///         // Define and initialize GTM.Modules here, specifying their socket numbers.        
    ///         GTM.Sytech.UsbDevice usbClient = new UsbDevice(1);
    ///         GTM.Sytech.Accel3Axis acel3Axis = new Accel3Axis(4);
    ///         GTM.Sytech.LCDTouch lcdTouch = new  LCDTouch(10,9,8);
    ///        // This method is run when the mainboard is powered up or reset.   
    ///        void ProgramStarted()
    ///        {
    ///            // must init I2C
    ///            accel3Axis.InitI2C();
    ///            GT.Timer timer = new Timer(1000);
    ///            timer.Tick += new Timer.TickEventHandler(timer_Tick);
    ///
    ///            timer.Start();
    /// 
    ///             // must set mode
    ///            accel3Axis.SetMode(Accel3Axis.Mma7455Mode.modeMeasurement, Accel3Axis.Mma7455gSelect.g2);
    ///
    ///           
    ///            Debug.Print("Program Started");
    ///        }
    ///
    ///        void timer_Tick(Timer timer)
    ///        {
    ///            short x = 0;
    ///            short y = 0;
    ///            short z = 0;
    ///            if (accel3Axis.ReadAll(ref x, ref y, ref z))
    ///            {
    ///                lcdTouch.SimpleGraphics.ClearNoRedraw();
    ///                lcdTouch.SimpleGraphics.DisplayText("X Axis :" + x, Resources.GetFont(Resources.FontResources.NinaB), Color.White, 10, 30);
    ///                lcdTouch.SimpleGraphics.DisplayText("Y Axis :" + y, Resources.GetFont(Resources.FontResources.NinaB), Color.White, 10, 60);
    ///                lcdTouch.SimpleGraphics.DisplayText("Z Axis :" + z, Resources.GetFont(Resources.FontResources.NinaB), Color.White, 10, 90);
    ///            }
    ///        }
    ///    }
    ///}

    /// 
    /// </code>
    /// </example>
    public partial class Accel3Axis : GTM.Module
    {
        // This example implements  a driver in managed code for a simple Gadgeteer module.  The module uses a 
        // single GTI.InterruptInput to interact with a sensor that can be in either of two states: low or high.
        // The example code shows the recommended code pattern for exposing the property (IsHigh). 
        // The example also uses the recommended code pattern for exposing two events: Accel3AxisHigh, Accel3AxisLow. 
        // The triple-slash "///" comments shown will be used in the build process to create an XML file named
        // GTM.ManufacturerName.Accel3Axis. This file will provide Intellisense and documention for the
        // interface and make it easier for developers to use the Accel3Axis module. 
       
        private GTI.I2CBus i2c;
        private Socket socket;
        // Note: A constructor summary is auto-generated by the doc builder.
        /// <summary></summary>
        /// <param name="socketNumber">The socket that this module is plugged in to.</param>
        public Accel3Axis(int socketNumber)
        {
            // This finds the Socket instance from the user-specified socket number.  
            // This will generate user-friendly error messages if the socket is invalid.
            // If there is more than one socket on this module, then instead of "null" for the last parameter, 
            // put text that identifies the socket to the user (e.g. "S" if there is a socket type S)
            socket = Socket.GetSocket(socketNumber, true, this, null);

            

            
            

        }

        /// <summary>
        /// Initialise the I2C bus
        /// </summary>
        public void InitI2C()
        {
            i2c = new I2CBus(socket, 0x1d, 100, this);
        }

        




        

         

        
        
    }
}
